/* Original Author:  Dominick O'Dierno
 * Date: 2007   odiernod@gmail.com     --Originally intended for use with DOMercury:  http://www.odierno.com/domercury
 * Description: Provides actions for folder items
 * 
 * --CHANGELOG--
 * 
 * */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using DOMercury.Interfaces;
using DOMercury.Items;
using DOMercury.Managers;

namespace DOMercury.Actions
{
    class FolderCmdOpenAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "Open in Command Shell";
            }
        }

        public string Description
        {
            get
            {
                return "Opens the Path in a Command Shell";
            }
        }
        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            foreach (PathItem p in ((ItemsList)item).Items)
            {
                try
                {
                    Process.Start("Cmd.exe",  "/k pushd " + @p.Path);
                    
                }
                catch(Exception ex)
                {
                    if (ConfigurationManager.ShowFailAlerts)
                    {
                        System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                    }
                }
            }
            return true;
        }

        public string ParamDescription
        {
            get
            {
                return "Unused";
            }
        }

        #endregion
    }

    class FolderExploreAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "Explore";
            }
        }

        public string Description
        {
            get
            {
                return "Opens the Paths in Windows Explorer";
            }
        }
       public string ParameterType
       {
           get
           {
               return "CommandLine";
           }
       }

        public bool Perform(IItem item, object specialParams)
        {

            foreach (PathItem p in ((ItemsList)item).Items)
            {
                try
                {
                    Process.Start("explorer.exe", p.Path);
                    //System.Windows.Forms.MessageBox.Show(p.Path);
                }
                catch (Exception ex)
                {
                    if (ConfigurationManager.ShowFailAlerts)
                    {
                        System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                    }
                }
            }
            return true;
        }

        public string ParamDescription
        {
            get
            {
                return "Unused";
            }
        }

        #endregion
    }

    class FolderExploreFolderAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "Explore Parent Folder";
            }
        }

        public string Description
        {
            get
            {
                return "Opens the Folder's Parent Folder in Windows Explorer";
            }
        }
        public string ParameterType
        {
            get
            {
                return "Unused";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            
                try
                {
                    Process.Start("explorer.exe", "/e,/select, " + ((ItemsList)(item)).Items[0].Path);
                    //System.Windows.Forms.MessageBox.Show(p.Path);
                    return true;
                }
                catch (Exception ex)
                {
                    if (ConfigurationManager.ShowFailAlerts)
                    {
                        System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                    }
                    return false;
                }
            
            
        }

        public string ParamDescription
        {
            get
            {
                return "Unused";
            }
        }

        #endregion
    }

    class FolderMoveAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "Move";
            }
        }

        public string Description
        {
            get
            {
                return "Moves the Folder(s) to the given parameter directory";
            }
        }
        public string ParameterType
        {
            get
            {
                return "Path";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                foreach (PathItem p in ((ItemsList)item).Items)
                {
                    
                        Directory.Move(p.Path, PathFinder.FindPath((string)specialParams) + "\\" + p.DisplayName);
                        
                    
                }
                return true;
            }
            catch (Exception ex)
            {
                if (ConfigurationManager.ShowFailAlerts)
                {
                    System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                }
                return false;
            
            }
            
        }

        public string ParamDescription
        {
            get
            {
                return "Directory to Move To";
            }
        }

        #endregion
    }

    class FolderDeleteAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "Delete";
            }
        }

        public string Description
        {
            get
            {
                return "Deletes the Folder(s)";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                foreach (PathItem p in ((ItemsList)item).Items)
                {
                    
                        Directory.Delete(p.Path);
                        
                }
                return true;

            }
            catch (Exception ex)
            {
                if (ConfigurationManager.ShowFailAlerts)
                {
                    System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                }
                return false;
            }
            
        }
        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public string ParamDescription
        {
            get
            {
                return "Unused";
            }
        }

        #endregion
    }

    class FolderNewFolderAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "New Folder";
            }
        }

        public string Description
        {
            get
            {
                return "Creates a new folder within the folder";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {

                string param = "NewFolder";
                string pin = (string)specialParams;
                if (pin.Trim() != "")
                {
                    param = pin.Trim();
                }
                foreach (PathItem p in ((ItemsList)item).Items)
                {

                    Directory.CreateDirectory(p.Path + "/" + param);

                }
                return true;

            }
            catch (Exception ex)
            {
                if (ConfigurationManager.ShowFailAlerts)
                {
                    System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                }
                return false;
            }

        }
        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public string ParamDescription
        {
            get
            {
                return "Optional Folder Name";
            }
        }

        #endregion
    }
    #region DirectoryCopy needs to be fixed
    //class FolderCopyAction : Interfaces.IAction
    //{
    //    #region IAction Members

    //    public string ActionType
    //    {
    //        get
    //        {
    //            return "copyto";
    //        }
    //    }

    //    public string Description
    //    {
    //        get
    //        {
    //            return "Copies the Folder(s) to the parameter directory";
    //        }
    //    }
    //    public ParamType ParameterType
    //    {
    //        get
    //        {
    //            return ParamType.Path;
    //        }
    //    }

    //    public bool Perform(IItem item, object specialParams)
    //    {
    //        foreach (PathItem p in ((ItemsList)item).Items)
    //        {
    //            try
    //            {
    //                File.Copy(p.Path, (string)specialParams + "\\" + p.DisplayItem);
    //            }
    //            catch
    //            {
    //            }
    //        }
    //        return true;
    //    }

    //    public string ParamDescription
    //    {
    //        get
    //        {
    //            return "Directory to be copied to";
    //        }
    //    }

    //    #endregion
    //}
    #endregion

    class FolderRenameAction : Interfaces.IAction
    {
        #region IAction Members

        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public string ActionType
        {
            get
            {
                return "Rename";
            }
        }

        public string Description
        {
            get
            {
                return "Renames the Path(s) to the parameter name";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                int i = 0;
                string directory = Path.GetDirectoryName(((ItemsList)(item)).Items[0].Path);
                
                    foreach (PathItem p in ((ItemsList)item).Items)
                    {
                        {
                            if (i == 0)
                            {
                                Directory.Move(p.Path, directory + "\\" + (string)specialParams);
                            }
                            else
                            {
                                Directory.Move(p.Path, directory + "\\" + Path.GetFileNameWithoutExtension((string)specialParams) + i.ToString());
                            }
                        }
                        i++;
                    }
                    return true;
                
            }
            catch (Exception ex)
            {
                if (ConfigurationManager.ShowFailAlerts)
                {
                    System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                }
                return false;
            }
               
        }

        public string ParamDescription
        {
            get
            {
                return "New Filename";
            }
        }

        #endregion
    }
    #region SendTo needs to be fixed
    //class FolderSendToAction : Interfaces.IAction
    //{
    //    #region IAction Members

    //    public ParamType ParameterType
    //    {
    //        get
    //        {
    //            return ParamType.SendTo;
    //        }
    //    }

    //    public string ActionType
    //    {
    //        get
    //        {
    //            return "send to";
    //        }
    //    }

    //    public string Description
    //    {
    //        get
    //        {
    //            return "Sends the folder to an item in the send to folder";
    //        }
    //    }

    //    public bool Perform(IItem item, object specialParams)
    //    {
    //        int i = 0;
    //        string directory = Path.GetDirectoryName(((ItemsList)(item)).Items[0].Path);
    //        foreach (PathItem p in ((ItemsList)item).Items)
    //        {
    //            try
    //            {
    //                string path = ((string)(specialParams));
    //                Process.Start(path, @p.Path.Replace (@"\\", @"\"));
    //            }
    //            catch
    //            {

    //            }
    //            i++;
    //        }
    //        return true;
    //    }

    //    public string ParamDescription
    //    {
    //        get
    //        {
    //            return "Send To Item";
    //        }
    //    }

    //    #endregion
    //}
    #endregion
}
